//时间格式转换为时间戳
function js_strto_time(time) {
    var new_str = time.replace(/:/g, '-');
    new_str = new_str.replace(/ /g, '-');
    new_str = new_str.replace(/\//g, '-');
    var arr = new_str.split("-");
    var datum = new Date(Date.UTC(arr[0], arr[1] - 1, arr[2], arr[3] - 8, arr[4], arr[5]));
    return strtotime = datum.getTime() / 1000;
}
//时间戳数据转换为时间格式
function js_date_time(unixtime) {
    var timestr = new Date(parseInt(unixtime)*1000);
    var year= timestr.getFullYear(); 
    var month= timestr.getMonth()+1; 
    var date= timestr.getDate(); 
    var hour= timestr.getHours(); 
    var minute= timestr.getMinutes(); 
    var second= timestr.getSeconds(); 
    var datetime = year+"/"+month+"/"+date+" "+hour+":"+minute+":"+second; 
    return datetime;
}

//获取url参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}

//返回上一级
function previous() {
    window.location = "../policyList.html?type=" + type + "&id=" + DepotID + "&name=" + base64.encode(DepotName) + "";
}

// 全局策略对象
var PolicyObj = {
    MessageSerialNo: "111"
};

var base64 = new Base64();
var type = getQueryString("type");
var DepotID = getQueryString("DepotID");
var DepotName = getQueryString("DepotName");
DepotName = base64.decode(DepotName);

function save() {
    var form = $(".main-container .form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    if (parseInt($("#MAppID").val()) == 0 && $.trim($("#MAppName").val()) == "") {
        alert("请先输入应用名称");
        return;
    }

    var comment = $(".policy_desc").val();
    var policy = generateParameter();
    policy = JSON.stringify(policy);

    $.ajax({
        method: "POST",
        async: false,
        url: "../../action/policy/create.do",
        data: {comment: comment, policy: policy, type: type, instance: DepotName},
        dataType: "json",
        success: function(data) {
            if (data.result == "true") {
                alert("添加成功！");
                previous();
            } else {
                alert("添加失败!")
            }
        }
    });
}

function edit() {
    var form = $(".main-container .form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    if (parseInt($("#MAppID").val()) == 0 && $.trim($("#MAppName").val()) == "") {
        alert("请先输入应用名称");
        return;
    }

    var comment = $(".policy_desc").val();
    var policy = generateParameter();
    policy = JSON.stringify(policy);

    $.ajax({
        method: "POST",
        async: false,
        url: "../../action/policy/update.do",
        data: {messageNo: id, comment: comment, policy: policy, type: type, instance: DepotName},
        dataType: "json",
        success: function(data) {
            if (data.result == "true") {
                alert("添加成功！");
                previous();
            } else {
                alert("添加失败!")
            }
        }
    });
}

function generateParameter() {
    PolicyObj.AppType = $(".MAppType").val();
    PolicyObj.AppID = $(".MAppID").val();
    PolicyObj.AppName = $(".MAppName").val();
    PolicyObj.UserType = $("#UserType").val();
    PolicyObj.R_StartTime = js_strto_time($(".M_StartTime").val());
//    PolicyObj.R_StartTime = js_strto_time(PolicyObj.R_StartTime) + "";
    PolicyObj.R_EndTime = js_strto_time($(".M_EndTime").val());
//    PolicyObj.R_EndTime = js_strto_time(PolicyObj.R_EndTime) + "";
    return PolicyObj;
}

function showData(resp) {
    var messageType = resp.MessageType;
    var messageNo = resp.MessageNo;
    var instanceName = resp.Instance;
    var comment = resp.Comment;
    PolicyObj = resp.Content;

    $(".MAppType").val(PolicyObj.AppType);
    $(".MAppID").val(PolicyObj.AppID);
    $(".MAppName").val(PolicyObj.AppName);
    $(".UserType").val(PolicyObj.UserType);
    $(".policy_desc").val(comment);
    $("#M_StartTime").val(js_date_time(parseInt(PolicyObj.R_StartTime)));
    $("#M_EndTime").val(js_date_time(parseInt(PolicyObj.R_EndTime)));
}

$(function() {
    //时间插件
    var start = {
        elem: '#M_StartTime',
        format: 'YYYY/MM/DD hh:mm:ss',
        istime: true,
        istoday: false,
        festival: true,
        choose: function(datas) {
            end.min = datas; //开始日选好后，重置结束日的最小日期
            end.start = datas //将结束日的初始值设定为开始日
        }
    };
    var end = {
        elem: '#M_EndTime',
        format: 'YYYY/MM/DD hh:mm:ss',
        istime: true,
        istoday: false,
        festival: true,
        choose: function(datas) {
            start.max = datas; //结束日选好后，重置开始日的最大日期
        }
    };
    laydate(start);
    laydate(end);

//    setInterval(function () {
//        parent.$("#page-content").attr("height", $("html").height());
//    }, 500);
})
function show_oldPolicy(array, number) {
    var souData = array[number];
    $(".header").text("当前策略在" + souData.CreateTime + "创建,ID为" + souData.MessageNo + ",版本号为" + souData.MessageSerialNo + ",属于" + souData.Instance + "策略库");

    showData(souData);
}

/*
 *获取所有版本的策略内容 
 */
function getAllPolicy() {
    $.ajax({
        type: "GET",
        async: false,
        url: "../../action/policy/history_policy_info.do",
        data: {type: type, instance: getQueryString("DepotName"), messageNo: id},
        dataType: "json",
        success: function(data) {
            policyArray = data;
            policyAmount = data.length
        }
    });
}